/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    string tree2str(TreeNode* t) {
        if(!t) return "";
        string result = "";
        result += to_string(t -> val);
        // cout<<result<<endl;
        if(t -> left && t -> right)
            result += "(" + tree2str(t -> left) + ")(" + tree2str(t -> right) + ")";
        else if(t -> left && !t -> right)
            result += "(" + tree2str(t -> left) + ")";
        else if(!t -> left && t -> right)
            result += "()(" + tree2str(t -> right) + ")";
        return result;
    }
};
